﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Transport
{
    public class TaiXeControl
    {
        transportEntities db;

        public TaiXeControl()
        {
            db = new transportEntities();
        }

        public List<tai_xe> listAll()
        {
            try
            {
                return db.tai_xe.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public tai_xe findById(string Id)
        {
            try
            {
                return db.tai_xe.SingleOrDefault(t => t.ma_tai_xe == Id);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<tai_xe> findByName(string Name)
        {
            try
            {
                return db.tai_xe.Where(t => t.ten == Name).ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool insert(tai_xe value)
        {
            try
            {
                db.tai_xe.AddObject(value);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool update(tai_xe value)
        {
            try
            {
                tai_xe st = db.tai_xe.SingleOrDefault(t => t.ma_tai_xe == value.ma_tai_xe);
                st = value;
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool updateCount(string id, int count)
        {
            try
            {
                tai_xe st = db.tai_xe.SingleOrDefault(t => t.ma_tai_xe == id);
                st.so_chuyen = count;
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool updateCount()
        {
            try
            {
                PhanCongControl PCctrl = new PhanCongControl();
                List<tai_xe> list = listAll();

                foreach (tai_xe tx in list)
                {
                    int count = PCctrl.count(tx.ma_tai_xe);
                    updateCount(tx.ma_tai_xe, count);
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool delete(string Id)
        {
            try
            {
                tai_xe st = db.tai_xe.SingleOrDefault(t => t.ma_tai_xe == Id);
                db.tai_xe.DeleteObject(st);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public List<LichDTO> getSchedule(string driverid)
        {
            return (from a in db.phan_cong
                    from b in db.chi_tiet_dat_xe
                    where a.ma_dat_xe == b.id && a.ma_tai_xe == driverid
                    select new LichDTO
                    {
                        MaDatXe = a.ma_tai_xe,
                        BienSoXe = a.bien_so_xe,
                        TuNgay = b.tu_ngay,
                        DenNgay = b.den_ngay,
                        GioDon = b.gio_don,
                        DiaDiemDon = b.dia_diem_don
                    }).ToList();
        }
    }
}